<template>
  <el-dialog :model-value="show" title="添加角色" @close="$emit('hide')">
    <el-form ref="formRef" :model="form" :rules="rules">
      <el-form-item label="账号" prop="username">
        <el-input
          v-model="form.username"
          autocomplete="off"
          placeholder="请输入账号"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="nickName">
        <el-input
          v-model="form.nickName"
          autocomplete="off"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="form.email"
          autocomplete="off"
          placeholder="请输入邮箱"
        />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="form.password"
          type="password"
          autocomplete="off"
          placeholder="请输入密码"
          :disabled="props.id !== 0"
        />
      </el-form-item>
      <el-form-item label="备注" prop="note">
        <el-input
          v-model="form.note"
          autocomplete="off"
          :rows="4"
          type="textarea"
          placeholder="请输入备注"
        />
      </el-form-item>
      <el-form-item label="是否启用：">
        <el-radio-group v-model="form.status" class="ml-4">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submit"> 确定 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { ElMessage, ElMessageBox, FormInstance } from "element-plus";
import { reactive, ref, watch } from "vue";
import { addUserApi, getUserByIdApi, updateUserApi } from "./api";

const props = defineProps({
  show: {
    type: Boolean,
    default: false,
  },
  id: {
    type: Number,
    default: 0,
  },
});
const formRef = ref<FormInstance>();
const emit = defineEmits(["hide"]);

watch(
  () => props.id,
  () => {
    if (props.id !== 0) {
      getUserByIdApi(props.id).then((res) => {
        if (res.code === 200) {
          Object.assign(form, res.data);
        }
      });
    }
  }
);
const validateEmail = (rule: any, value: string, cb: any) => {
  if (!value) {
    return cb(new Error("邮箱格式不正确"));
  } else {
    return cb();
  }
};
const cancel = () => {
  emit("hide");
  formRef.value?.resetFields();
};

const addUser = () => {
  ElMessageBox.confirm("确定要添加用户吗", "添加用户", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  })
    .then(() => {
      addUserApi(form).then((res) => {
        if (res.code === 200) {
          ElMessage.success("添加成功");
          emit("hide", true);
          formRef.value?.resetFields();
        }
      });
    })
    .catch(() => {});
};

const editUser = () => {
  ElMessageBox.confirm("确定要修改用户吗", "修改用户", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      updateUserApi(form).then((res) => {
        if (res.code === 200) {
          ElMessage.success("修改成功");
          emit("hide", true);
          formRef.value?.resetFields();
        }
      });
    })
    .catch(() => {});
};

const submit = () => {
  formRef.value?.validate((ok: Boolean) => {
    if (ok) {
      if (props.id === 0) {
        addUser();
      } else {
        editUser();
      }
    } else {
    }
  });
};
const rules = {
  username: [{ required: true, message: "请输入账号", trigger: "blur" }],
  nickName: [{ required: true, message: "请输入姓名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  note: [{ required: false, message: "", trigger: "blur" }],
  email: [{ required: true, validator: validateEmail }],
};
const form = reactive<List>({
  username: "",
  nickName: "",
  email: "",
  password: "",
  note: "",
  status: 0,
});
</script>
<style lang="less" scoped></style>
